﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Collections;
using GPCSC.Web;
using NickLee.Common;
using System.Text;
using System.IO;

namespace GPCSC.Web.sjsbyes
{
    public partial class ZTJ : PageBase
    {

         public  double[] ad = new double[26];
         public StringBuilder[] adds = new StringBuilder[26];
         public string add = "0";


         public double[] su = new double[26];
         public StringBuilder[] sums = new StringBuilder[26];
         public string asum = "0";
        public string  cout = "";
        protected void Page_Load(object sender, EventArgs e)
        {

            for (int i = 0; i < 26; i++)
            {
                adds[i] = new StringBuilder();
                sums[i] = new StringBuilder();
            }
            if (!IsPostBack)
            {


                load();

            }
        }
        protected void load()
        {

            Model.CL_USER_S users = reuser();
            string postid = users.CL_POSTID_PK.ToString();
            string postname = users.POSTName.ToString();
            this.Label1.Text = users.POSTName.ToString();
            this.Label2.Text = DateTime.Now.Month.ToString();
            f_buildGrid(postid,postname);

        }

        protected Model.CL_USER_S reuser()
        {
            byte[] userInfo = (byte[])base.o_baseTable[c_obj.s_userInfo];
            Hashtable userInfoTable = CompressData.UnCompressHashtable(userInfo);
            Model.CL_USER_S users = (Model.CL_USER_S)userInfoTable["SYS_USER"];
            return users;

        }
        protected void f_buildGrid(string postid,string postname)
        {
            this.postid.Value = postid;
            this.postName.Value = postname; 
            DataSet dsname = new Summ().PostUser(postid);
            sumss(dsname, "newSelAllbyuserid_km", DateTime.Now.Month.ToString(), DateTime.Now.Month.ToString(),DateTime.Now.Year);
           
        }
        protected void sumss(DataSet dsname, string sql, string kmonth,string jmonth,int year)
        {
            if (kmonth == jmonth)
            {
                Label2.Text = kmonth;
            }
            else
            {
                Label2.Text =kmonth+"月至"+jmonth;
            }

            if (dsname != null)
            {
                if (dsname.Tables.Count > 0)
                {
                    if (dsname.Tables[0].Rows.Count > 0)
                    {
                        PostListS.DataSource = dsname;
                        PostListS.DataBind();
                        cout = dsname.Tables[0].Rows.Count.ToString();
                        //循环遍历每个领导人
                        foreach (DataRow dr in dsname.Tables[0].Rows)
                        {
                            string userid = dr["CL_USER_SOID"].ToString();
                            Hashtable htables = new Hashtable();
                            htables.Add("@lgid", userid);
                            htables.Add("@year", year);
                            htables.Add("@kmonth", kmonth);
                            htables.Add("@jmonth", jmonth);
                            DataSet ds = new SqlReadXml().ReaderDataSet(sql, htables);
                            if (ds != null)
                            {
                                if (ds.Tables.Count > 0)
                                {
                                    if (ds.Tables[0].Rows.Count > 0)
                                    {
                                        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                                        {
                                            ad[0] += Usual.f_convertDobule(ds.Tables[0].Rows[i]["zday"].ToString());//下基层活动天数
                                            ad[1] += Usual.f_convertDobule(ds.Tables[0].Rows[i]["XJCDYTS"].ToString());//下基层调研
                                            ad[2] += Usual.f_convertDobule(ds.Tables[0].Rows[i]["ZXMQRJPS"].ToString());//民情
                                            ad[3] += Usual.f_convertDobule(ds.Tables[0].Rows[i]["ZCRHTS"].ToString());//走村入户
                                            ad[4] += Usual.f_convertDobule(ds.Tables[0].Rows[i]["ZDZHTS"].ToString());//驻点住户
                                            ad[5] += Usual.f_convertDobule(ds.Tables[0].Rows[i]["GKJFTS"].ToString());//公开接访天数
                                            ad[6] += Usual.f_convertDobule(ds.Tables[0].Rows[i]["JDXFAJ"].ToString());//接待信访件数
                                            ad[7] += Usual.f_convertDobule(ds.Tables[0].Rows[i]["yjaxf"].ToString());//已结案
                                            ad[8] += Usual.f_convertDobule(ds.Tables[0].Rows[i]["ZDXFBAJS"].ToString());//重点信访
                                            ad[9] += Usual.f_convertDobule(ds.Tables[0].Rows[i]["ZDXFBAJWCS"].ToString());//重点信访包案。。
                                            ad[10] += Usual.f_convertDobule(ds.Tables[0].Rows[i]["KNQZZHS"].ToString());//已走访困难
                                            ad[11] += Usual.f_convertDobule(ds.Tables[0].Rows[i]["WWKNQZQK_YZFHS"].ToString());//已走访挂钩。。
                                            ad[12] += Usual.f_convertDobule(ds.Tables[0].Rows[i]["YBFHS"].ToString());// 已帮扶挂钩。。
                                            ad[13] += Usual.f_convertDobule(ds.Tables[0].Rows[i]["WWJZZHLJJE"].ToString());//慰问捐助金额。。
                                            ad[14] += Usual.f_convertDobule(ds.Tables[0].Rows[i]["ZKZTHCS"].ToString());
                                            ad[15] += Usual.f_convertDobule(ds.Tables[0].Rows[i]["Z_CJRC"].ToString());
                                            ad[16] += Usual.f_convertDobule(ds.Tables[0].Rows[i]["XSZCXJCS"].ToString());
                                            ad[17] += Usual.f_convertDobule(ds.Tables[0].Rows[i]["X_CJRC"].ToString());
                                            ad[18] += Usual.f_convertDobule(ds.Tables[0].Rows[i]["CJLDTS"].ToString());
                                            ad[19] += Usual.f_convertDobule(ds.Tables[0].Rows[i]["WJCDWFCZJLJJE"].ToString());
                                            ad[25] += Usual.f_convertDobule(ds.Tables[0].Rows[i]["dybg"].ToString());
                                        }
                                        for (int i = 0; i < 20; i++)
                                        {
                                            adds[i].Append("<td  class=\"td\" >" + ad[i].ToString() + "</td>");

                                            su[i] += ad[i];
                                            ad[i] = 0;
                                        }
                                        su[25] += ad[25];
                                        adds[25].Append("<td  class=\"td\" >" + ad[25].ToString() + "</td>");
                                        ad[25] = 0;
                                    }
                                    else
                                    {
                                        for (int m = 0; m < 20; m++)
                                        {

                                            ad[m] = 0;
                                            adds[m].Append("<td  class=\"td\" >" + add + "</td>");
                                        }
                                        ad[25] = 0;
                                        adds[25].Append("<td  class=\"td\" >" + add + "</td>");
                                    }
                                }
                            }
                            adwt(dr["CL_USER_SOID"].ToString(), kmonth, jmonth, year);//问题总计
                            jjwt(dr["CL_USER_SOID"].ToString(), kmonth, jmonth,year);//解决问题统计
                            Bss(dr["CL_USER_SOID"].ToString(), kmonth, jmonth,year);//办实事
                            knAgg(dr["CL_USER_SOID"].ToString());


                        }
                        //合计值
                        for (int i = 0; i < 26; i++)
                        {

                            sums[i].Append("<td  class=\"td\" >" + su[i].ToString() + "</td>");
                        }
                    }
                }
            }
        }

        // 问题统计
        protected void adwt(string userid ,string kmonth,string jmonth,int year)
        {
            int wts = 0;
            Hashtable htable = new Hashtable();
            htable.Add("@lgid", userid);
            htable.Add("@year", year);
            htable.Add("@kmonth",kmonth);
            htable.Add("@jmonth", jmonth);
            DataSet dwts = new SqlReadXml().ReaderDataSet("countWT",htable);
            if (dwts.Tables[0].Rows.Count > 0)
            {
                wts = Usual.f_convertInt32(dwts.Tables[0].Rows[0]["wts"].ToString());
            }
            adds[20].Append("<td  class=\"td\" >" + wts.ToString() + "</td>");
            su[20] += wts;
        }

        //解决问题统计
        protected void jjwt(string userid, string kmonth, string jmonth,int year)
        {
            int wts = 0;
            Hashtable htable = new Hashtable();
            htable.Add("@lgid", userid);
            htable.Add("@year", year);
            htable.Add("@kmonth", kmonth);
            htable.Add("@jmonth", jmonth);
            DataSet dwts = new SqlReadXml().ReaderDataSet("countjjWT",htable);
            if (dwts.Tables[0].Rows.Count > 0)
            {
                wts = Usual.f_convertInt32(dwts.Tables[0].Rows[0]["jwts"].ToString());
            }
            adds[21].Append("<td  class=\"td\" >" + wts.ToString() + "</td>");
            su[21] += wts;
        }
        //办实事情况
        protected void Bss(string userid, string kmonth, string jmonth,int year)
        {
            int wts = 0;
            Hashtable htable = new Hashtable();
            htable.Add("@lgid", userid);
            htable.Add("@year", year);
            htable.Add("@kmonth", kmonth);
            htable.Add("@jmonth", jmonth);
            DataSet dwts = new SqlReadXml().ReaderDataSet("counBss",htable);
            if (dwts.Tables[0].Rows.Count > 0)
            {
                wts = Usual.f_convertInt32(dwts.Tables[0].Rows[0]["jwts"].ToString());
            }
            adds[22].Append("<td  class=\"td\" >" + wts.ToString() + "</td>");
            su[22] += wts;
        }

        //困难及挂钩结对户数
        protected void knAgg(string userid)
        { 
            int kns = 0;
            int ggs = 0;
            Hashtable htable = new Hashtable();
            htable.Add("@lgid", userid);
            DataSet dwts = new SqlReadXml().ReaderDataSet("selks", htable);
            if (dwts.Tables[0].Rows.Count > 0)
            {
               
                foreach (DataRow dr in dwts.Tables[0].Rows)
                {
                    kns += Usual.f_convertInt32(dr["knqzzhs"].ToString());
                    ggs += Usual.f_convertInt32(dr["ggjdhs"].ToString());

                }
            }
            adds[23].Append("<td  class=\"td\" >" + kns.ToString() + "</td>");
            su[23] += kns;
            adds[24].Append("<td  class=\"td\" >" + ggs.ToString() + "</td>");
            su[24] += ggs;
            
        }


        protected void CallBack1_Callback(object sender, NickLee.Web.UI.CallBackEventArgs e)
        {
            try
            {
                string types = e.Parameters[0].ToString();
                if (types == "1")//按季度
                {
                    string lgname = e.Parameters[1].ToString();
                    int jd = Usual.f_convertInt32(e.Parameters[2].ToString());
                    int year = Usual.f_convertInt32(e.Parameters[3].ToString());
                    int kmonth = 0;
                    int jmonth = 0;
                    switch (jd)
                    {
                        case 1:
                            kmonth = 1;
                            jmonth = 3;
                            break;
                        case 2:
                            kmonth = 4;
                            jmonth = 6;
                            break;
                        case 3:
                            kmonth = 7;
                            jmonth = 9;
                            break;
                        default:
                            kmonth = 10;
                            jmonth = 12;
                            break;
                    }
                    kbymont(lgname, kmonth,jmonth,year);

                }
                else if (types == "2")//按月份
                {
                    string lgname = e.Parameters[1].ToString();
                    int kmonth = Usual.f_convertInt32(e.Parameters[2].ToString());
                    int jmonth = Usual.f_convertInt32(e.Parameters[3].ToString());
                    int count = e.Parameters.Length;
                    int year = 0;
                    if (count > 4)
                    {
                        year = Usual.f_convertInt32(e.Parameters[4].ToString());
                    }
                    else
                    {
                        year = DateTime.Now.Year;
                    }
                    
                    kbymont(lgname, kmonth, jmonth,year);

                }
                #region 只按领导人姓名查询
                else
                {
                    string lgname = e.Parameters[1].ToString().Trim();
                    lgname = "%" + lgname + "%";
                    Hashtable htable = new Hashtable();
                    htable.Add("@CL_POST_SOID", this.postid.Value);
                    htable.Add("@USERNAME", lgname);
                    DataSet dsname = new DataReturn().ReturnData(htable, "newSelAll_m");
                    sumss(dsname, "newSelAllbyuserid_km", DateTime.Now.Month.ToString(), DateTime.Now.Month.ToString(),DateTime.Now.Year);
                }
                #endregion
            }
            catch (Exception ee)
            {

            }
            finally
            {
                Panel1.RenderControl(e.Output);
            }
        }



        //汇总上报
        protected void LCallBack_Callback(object sender, NickLee.Web.UI.CallBackEventArgs e)
        {
        //    try
        //    {
        //        Hashtable htable = new Hashtable();
        //        htable.Add("@CL_POST_SOID", this.postid.Value);
        //        DataSet dsname = new DataReturn().ReturnData(htable, "newSelAll");
        //        cout = dsname.Tables[0].Rows.Count.ToString();
        //        if (dsname != null)
        //        {
        //            if (dsname.Tables.Count > 0)
        //            {
        //                if (dsname.Tables[0].Rows.Count > 0)
        //                {
        //                    //循环遍历每个领导人
        //                    foreach (DataRow dr in dsname.Tables[0].Rows)
        //                    {
        //                        string userid = dr["CL_USER_SOID"].ToString();
        //                        Hashtable htables = new Hashtable();
        //                        htables.Add("@lgid", userid);
        //                        htables.Add("@year",DateTime.Now.Year);
        //                        htables.Add("@month", DateTime.Now.Month);
        //                        DataSet ds = new SqlReadXml().ReaderDataSet("newSelAllbyuserid_m", htables);
        //                        if (ds != null)
        //                        {
        //                            if (ds.Tables.Count > 0)
        //                            {
        //                                if (ds.Tables[0].Rows.Count > 0)
        //                                {
        //                                    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
        //                                    {
        //                                        ad[1] += Usual.f_convertDobule(ds.Tables[0].Rows[i]["XJCDYTS"]);//下基层
        //                                        ad[2] += Usual.f_convertDobule(ds.Tables[0].Rows[i]["ZXMQRJPS"]);//民情
        //                                        ad[3] += Usual.f_convertDobule(ds.Tables[0].Rows[i]["ZCRHTS"]);//走村入户
        //                                        ad[4] += Usual.f_convertDobule(ds.Tables[0].Rows[i]["ZDZHTS"]);//驻点住户
        //                                        ad[5] += Usual.f_convertDobule(ds.Tables[0].Rows[i]["GKJFTS"]);//公开接访天数
        //                                        ad[6] += Usual.f_convertDobule(ds.Tables[0].Rows[i]["JDXFAJ"]);//接待信访件数
        //                                        ad[7] += Usual.f_convertDobule(ds.Tables[0].Rows[i]["yjaxf"]);//已结案

        //                                        ad[8] += Usual.f_convertDobule(ds.Tables[0].Rows[i]["ZDXFBAJS"]);
        //                                        ad[9] += Usual.f_convertDobule(ds.Tables[0].Rows[i]["ZDXFBAJWCS"]);
        //                                        ad[10] += Usual.f_convertDobule(ds.Tables[0].Rows[i]["KNQZZHS"]);
        //                                        ad[11] += Usual.f_convertDobule(ds.Tables[0].Rows[i]["GGJDHS"]);
        //                                        ad[12] += Usual.f_convertDobule(ds.Tables[0].Rows[i]["WWKNQZQK_YZFHS"]);
        //                                        ad[13] += Usual.f_convertDobule(ds.Tables[0].Rows[i]["YBFHS"]);
        //                                        ad[14] += Usual.f_convertDobule(ds.Tables[0].Rows[i]["WWJZZHLJJE"]);
        //                                        ad[15] += Usual.f_convertDobule(ds.Tables[0].Rows[i]["ZKZTHCS"]);
        //                                        ad[16] += Usual.f_convertDobule(ds.Tables[0].Rows[i]["Z_CJRC"]);
        //                                        ad[17] += Usual.f_convertDobule(ds.Tables[0].Rows[i]["XSZCXJCS"]);
        //                                        ad[18] += Usual.f_convertDobule(ds.Tables[0].Rows[i]["X_CJRC"]);
        //                                        ad[19] += Usual.f_convertDobule(ds.Tables[0].Rows[i]["CJLDTS"]);
        //                                        ad[20] += Usual.f_convertDobule(ds.Tables[0].Rows[i]["WJCDWQZBSSZJS"]);
        //                                        ad[21] += Usual.f_convertDobule(ds.Tables[0].Rows[i]["WJCDWFCZJLJJE"]);
        //                                        ad[0] += Usual.f_convertDobule(ds.Tables[0].Rows[i]["zday"]);
        //                                    }
        //                                    for (int i = 0; i < 22; i++)
        //                                    {
        //                                        //adds[i].Append("<td  class=\"td\" >" + ad[i].ToString() + "</td>");
        //                                        su[i] += ad[i];
        //                                        ad[i] = 0;
        //                                    }
        //                                }
        //                                else
        //                                {
        //                                    for (int m = 0; m < 22; m++)
        //                                    {

        //                                        ad[m] = 0;
        //                                        adds[m].Append("<td  class=\"td\" >" + add + "</td>");
        //                                    }
        //                                }
        //                            }
        //                        }
        //                        //问题及解决情况统计
        //                        int wts = 0;
        //                        DataSet dwts = new SqlReadXml().ReaderDataSet("countWT_m", htables);
        //                        if (dwts.Tables[0].Rows.Count > 0)
        //                        {
        //                            wts = Usual.f_convertInt32(dwts.Tables[0].Rows[0]["wts"].ToString());
        //                        }
        //                        //adds[21].Append("<td  class=\"td\" >" + wts.ToString() + "</td>");
        //                        su[22] += wts;

        //                        int jwts = 0;
        //                        DataSet jdwts = new SqlReadXml().ReaderDataSet("countjjWT_m", htables);
        //                        if (jdwts.Tables[0].Rows.Count > 0)
        //                        {
        //                            jwts = Usual.f_convertInt32(jdwts.Tables[0].Rows[0]["jwts"].ToString());
        //                        }
        //                        //adds[22].Append("<td  class=\"td\" >" + jwts.ToString() + "</td>");
        //                        su[23] += jwts;



        //                    }
        //                    //上报合计值
        //                    Hashtable hzhatable = new Hashtable();
        //                    hzhatable.Add("postid", this.postid.Value);
        //                    hzhatable.Add("postName", this.postName.Value);
        //                    hzhatable.Add("zday", su[0]);
        //                    hzhatable.Add("xjc", su[1]);
        //                    hzhatable.Add("mqri", su[2]);
        //                    hzhatable.Add("zzrh", su[3]);
        //                    hzhatable.Add("zdzh", su[4]);
        //                    hzhatable.Add("gkjf", su[5]);
        //                    hzhatable.Add("jfxj", su[6]);
        //                    hzhatable.Add("yjaxf", su[7]);
        //                    hzhatable.Add("zdxfbajs", su[8]);
        //                    hzhatable.Add("zdxfbawcjs", su[9]);
        //                    hzhatable.Add("knqzzhs", su[10]);
        //                    hzhatable.Add("ggjdhs", su[11]);
        //                    hzhatable.Add("zzfhs", su[12]);
        //                    hzhatable.Add("ybfhs", su[13]);
        //                    hzhatable.Add("wedsd", su[14]);
        //                    hzhatable.Add("fdagr", su[15]);
        //                    hzhatable.Add("jffdbhxj", su[16]);
        //                    hzhatable.Add("jfvdxj", su[17]);
        //                    hzhatable.Add("jfsdxsj", su[18]);
        //                    hzhatable.Add("jfsdxj", su[22]);
        //                    hzhatable.Add("jfxcvj", su[23]);
        //                    hzhatable.Add("cjldcs", su[19]);
        //                    hzhatable.Add("jfxvcj", su[20]);
        //                    hzhatable.Add("jfxdsj", su[21]);
        //                    hzhatable.Add("year", DateTime.Now.Year.ToString());
        //                    hzhatable.Add("month", DateTime.Now.Month.ToString());
        //                    SQLServerDAL.databyhtable dh = new SQLServerDAL.databyhtable();

        //                    Hashtable htsd = new Hashtable();
        //                    htsd.Add("@postid", this.postid.Value);
        //                    htsd.Add("@year", DateTime.Now.Year.ToString());
        //                    htsd.Add("@month", DateTime.Now.Month.ToString());

        //                    DataSet dsw = new DataSet();
        //                    dsw = new SqlReadXml().ReaderDataSet("sel_sjhz_byym", htsd);
        //                    if (dsw.Tables[0].Rows.Count > 0)
        //                    {
        //                        dh.databyhtables(hzhatable, "update", "sjhz", "tableid", dsw.Tables[0].Rows[0]["tableid"].ToString());
        //                    }
        //                    else
        //                    {
        //                        dh.databyhtables(hzhatable, "add", "sjhz", "", "");
        //                    }
        //                }
        //            }
        //        }

        //    }
        //    catch (Exception ee)
        //    {
        //        throw ee;
        //    }
        //    finally
        //    {
        //        //load();
        //        LPanel.RenderControl(e.Output);
        //    }
        }


        //按月份查询
        protected void kbymont(string lgname, int kmonth,int jmonth,int year)
        {
           
            lgname = "%" + lgname + "%";
            Hashtable htable = new Hashtable();
            htable.Add("@CL_POST_SOID", this.postid.Value);
            htable.Add("@USERNAME", lgname);
            DataSet dsname = new DataReturn().ReturnData(htable, "newSelAll_m");
            sumss(dsname, "newSelAllbyuserid_km",kmonth.ToString(),jmonth.ToString(),year);
        }



        protected void Button1_Click(object sender, EventArgs e)
        {
            load();
            byte[] userInfo = (byte[])base.o_baseTable[c_obj.s_userInfo];
            Hashtable userInfoTable = CompressData.UnCompressHashtable(userInfo);
            Model.CL_USER_S users = (Model.CL_USER_S)userInfoTable["SYS_USER"];
            string filename = users.POSTName.ToString() + DateTime.Now.Year.ToString() + "年" + DateTime.Now.Month.ToString() + "月" + "三进三帮统计表.doc";
        Response.Clear();
        Response.BufferOutput = true;
        //设定输出的字符集
        Response.Charset = "GB2312";
        //假定导出的文件名为FileName.doc
        Response.AppendHeader("Content-Disposition", "attachment;filename=" + Server.UrlEncode(filename));
        Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
        /**/////设置导出文件的格式
        Response.ContentType = "application/ms-word";
        //关闭ViewState
        
        
       // GrwResult.EnableViewState = false;
        Panel1.EnableViewState = false;


        System.Globalization.CultureInfo cultureInfo = new System.Globalization.CultureInfo("ZH-CN", true);
        System.IO.StringWriter stringWriter = new System.IO.StringWriter(cultureInfo);
        System.Web.UI.HtmlTextWriter textWriter = new System.Web.UI.HtmlTextWriter(stringWriter);

        Panel1.RenderControl(textWriter);

        //保存到服务器中
        SQLServerDAL.databyhtable dtable = new SQLServerDAL.databyhtable();
        Hashtable htable = new Hashtable();
        htable.Add("fileUrl", stringWriter.ToString());

        #region  首先验证该月是否有 记录
      
        string year = DateTime.Now.Year.ToString();
        string month = DateTime.Now.Month.ToString();
        Hashtable htab = new Hashtable();
        htab.Add("@year",year);
        htab.Add("@month",month);
        htab.Add("@POstID",users.CL_POSTID_PK.ToString());
        DataSet ds = new SqlReadXml().ReaderDataSet("tifiled", htab);
        if (ds.Tables[0].Rows.Count > 0)
        {
            dtable.databyhtables(htable, "update", "TJfile", "FileID", ds.Tables[0].Rows[0]["FileID"].ToString());
        }
        else
        {
            htable.Add("filename", filename);
            htable.Add("TJdate",DateTime.Now);
            htable.Add("POstID",users.CL_POSTID_PK.ToString());
            htable.Add("PostName", users.POSTName.ToString());
            htable.Add("year",DateTime.Now.Year.ToString());
            htable.Add("month", DateTime.Now.Month.ToString());
            dtable.databyhtables(htable, "add", "TJfile", "", "");
        }
        #endregion

        

       // File.Copy(System.Web.HttpContext.Current.Server.MapPath("a.html"), System.Web.HttpContext.Current.Server.MapPath("a.doc"));

        // //把HTML写回浏览器
        Response.Write(stringWriter.ToString());
        Response.End();


        }
    }
}